package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IHomeBannerDAO;
import org.jsola.hr.dto.HomeBannerAddDTO;
import org.jsola.hr.dto.HomeBannerUpdateDTO;
import org.jsola.hr.entity.HomeBannerDO;
import org.jsola.hr.query.HomeBannerQuery;
import org.jsola.hr.service.IHomeBannerService;
import org.jsola.hr.vo.HomeBannerListVO;
import org.jsola.hr.vo.HomeBannerVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卫人云-首页Banner管理
 *
 * @author zhr
 */
@Service("hrHomeBannerServiceImpl")
public class HomeBannerServiceImpl implements IHomeBannerService {

    @Autowired
    private IHomeBannerDAO homeBannerDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public HomeBannerVO save(HomeBannerAddDTO homeBannerAddDTO, TokenUser tokenUser) {
        // 转DO
        HomeBannerDO homeBannerDO = homeBannerAddDTO.to(HomeBannerDO.class);
        // 保存
        homeBannerDO = save(homeBannerDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return homeBannerDO.to(HomeBannerVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(HomeBannerUpdateDTO homeBannerUpdateDTO, TokenUser tokenUser) {
        //转DO
        HomeBannerDO homeBannerDO = homeBannerUpdateDTO.to(HomeBannerDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(homeBannerDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... homeBannerIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) homeBannerIds);
    }


    @Override
    public HomeBannerVO selectById(String homeBannerId, String siteId) {
        HomeBannerDO homeBannerDO = selectDOById(homeBannerId, siteId);
        if (homeBannerDO == null) {
            return null;
        }
        return homeBannerDO.to(HomeBannerVO.class);
    }

    @Override
    public List<HomeBannerListVO> select(HomeBannerQuery homeBannerQuery, String siteId) {
        List<HomeBannerDO> homeBannerDOList = selectDO(homeBannerQuery, siteId);
        if(CollectionUtils.isEmpty(homeBannerDOList)) {
            return homeBannerDOList == null ? null : new ArrayList<>();
        }
        return homeBannerDOList.stream()
                .map(homeBannerDO -> homeBannerDO.to(HomeBannerListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(HomeBannerQuery homeBannerQuery, String siteId) {
        Example example = buildExample(homeBannerQuery,siteId);
        return homeBannerDAO.selectCountByExample(example);
    }

    @Override
    public Page<HomeBannerListVO> selectPage(HomeBannerQuery homeBannerQuery, String siteId) {
        Example example = buildExample(homeBannerQuery,siteId);
        Page<HomeBannerDO> page = homeBannerDAO.selectPageByExample(example,
                homeBannerQuery.getPageNo(),
                homeBannerQuery.getPageSize());

        return page.to(HomeBannerListVO.class);
    }

    @Override
    public HomeBannerDO selectDOById(String homeBannerId, String siteId) {
        return listById(homeBannerId, siteId);
    }

    @Override
    public List<HomeBannerDO> selectDO(HomeBannerQuery homeBannerQuery, String siteId) {
        Example example = buildExample(homeBannerQuery,siteId);
        return homeBannerDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param homeBannerQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(HomeBannerQuery homeBannerQuery, String siteId) {
        Example example = new Example(HomeBannerDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,homeBannerQuery.getOrders());
        return example;
    }
}




